package com.zcz.umengpush.mapper;

import com.zcz.umengpush.pojo.PushMsg;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 王卫帅
 */

public interface PushMsgMapper extends Mapper<PushMsg> {


    //测试
    List<PushMsg> callProcedure(Map map);


    /**
     *
     *获取推送的信息列表(不能超过495条数据,只推送近两个小时未推送的消息;当推送出现错误，允许重试2次，累计推送3次)
     * 每20秒读取一次
     * @return
     */

//    @Select("select g.groupid,\n" +
//            "             g.devicetype,g.usertype,\n" +
//            "             max(g.objectid) objectid,\n" +
//            "             max(g.objecttype) objecttype,\n" +
//            "             max(g.title) title,\n" +
//            "             max(g.content) content,\n" +
//            "             replace(xmlagg(xmlparse(content ',' || g.tokenid wellformed) order by 1)\n" +
//            "             .getclobval(),' ','')\n" +
//            "              tokens,\n" +
//            "               replace(xmlagg(xmlparse(content ',' || g.id wellformed) order by 1)\n" +
//            "             .getclobval(),' ','')ids\n" +
//            "        from (select m.*,\n" +
//            "                     row_number() over(order by m.objecttype desc, m.adddate, m.groupid) rn\n" +
//            "                from push_msg m\n" +
//            "               where (m.ispush = 0 or (m.ispush = 2 and m.retrynum < 3))\n" +
//            "                 and m.objecttype <> 18  \n" +
//
//            "                 and exists (select u.id\n" +
//            "                        from push_user p, web_user u,t_teacher b\n" +
//            "                       where (p.userid = u.id or p.userid = b.id)\n" +
//            "                         and p.tokenid = m.tokenid\n" +
//            "                         and (u.growingno like '006%'or b.growingno like '006%'))\n" +
//
//            "                 and m.pushdate > sysdate - 2 / 24 and m.pushdate < sysdate) g\n" +
//            "       where g.rn <= 495\n" +
//            "       group by g.groupid, g.devicetype,g.usertype,g.objectid")
    List<PushMsg> selectInfoList(Map map);
    /**
     *
     *获取推送的交流消息列表(不能超过495条数据,只推送近10分钟未推送的消息;当推送出现错误，允许重试2次，累计推送3次)
     * 每3秒读取一次
     * @return
     */

//    @Select("select g.groupid,\n" +
//            "             g.devicetype,g.usertype,\n" +
//            "             max(g.objectid) objectid,\n" +
//            "             max(g.objecttype) objecttype,\n" +
//            "             max(g.title) title,\n" +
//            "             max(g.content) content,\n" +
//            "             replace(xmlagg(xmlparse(content ',' || g.tokenid wellformed) order by 1)\n" +
//            "             .getclobval(),' ','')\n" +
//            "              tokens,\n" +
//            "               replace(xmlagg(xmlparse(content ',' || g.id wellformed) order by 1)\n" +
//            "             .getclobval(),' ','')ids\n" +
//            "        from (select m.*,\n" +
//            "                     row_number() over(order by m.adddate, m.groupid) rn\n" +
//            "                from push_msg m\n" +
//            "               where (m.ispush = 0 or (m.ispush = 2 and m.retrynum < 3))\n" +
//            "                 and m.objecttype = 18  \n" +
//
//            "                 and exists (select u.id\n" +
//            "                        from push_user p, web_user u,t_teacher b\n" +
//            "                       where (p.userid = u.id or p.userid = b.id)\n" +
//            "                         and p.tokenid = m.tokenid\n" +
//            "                         and (u.growingno like '006%'or b.growingno like '006%'))\n" +
//
//            "                 and m.pushdate > sysdate - 10 / 60 / 24 and m.pushdate < sysdate) g\n" +
//            "       where g.rn <= 495\n" +
//            "       group by g.groupid, g.devicetype,g.usertype,g.objectid")
    List<PushMsg> selectExchangeList(Map map);

    /**
     * 发送成功后批量更新消息状态和推送时间 发送次数
     */
    @Update("update push_msg set ISPUSH = 1,retrynum=retrynum+1,PushCompleteDate = #{PushCompleteDate,jdbcType=TIMESTAMP} where ID = #{id}")
    int updateSuccessPushMsg (String id, Date PushCompleteDate);
    /**
     * 发送失败后批量更新消息状态 和发送次数
     */
    @Update("update push_msg set ISPUSH = 2,retrynum=retrynum+1 where ID = #{id}")
    int updateFailPushMsg (String id);

    /**
     * 当数据 移动完成之后  删除 push_msg 表里昨天的数据
     * @return
     */
    @Delete("delete from push_msg where to_char(adddate,'YYYY-MM-DD')&lt;= to_char(SYSDATE-1,'YYYY-MM-DD') ")
    int del();
}
